﻿using FinalPointSet.Model.Model;
using FinalPointSet.View.Page.Knowledge;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Windows.Controls;

namespace FinalPointSet.Model.Util
{
    public class PageUtil : SingletonBase<PageUtil>
    {
        private Frame? PageFrame;
        private Dictionary<string, Page> Pages = new();
        private static readonly Dictionary<Type, System.Windows.Controls.Page> bufferPages = new();
        public const string FieldMappingPage = "字段映射";
        public const string DependencyPage = "依赖识别";
        public const string GroupingSetsPage = "维度组合";
        public const string MockDataPage = "模拟数据";
        public const string KnowledgePage = "知识点";
        public const string SettingPage = "设置";
        public const string FieldMappingSettingPage = "字段映射设置";
        public const string FieldMappingSettingDataSourcePage = "字段映射设置_数据源";
        public const string FieldMappingSettingMappingRulePage = "字段映射设置_映射规则";
        public const string FieldMappingSettingModelPage = "字段映射设置_模板";
        public const string MockDataSettingPage = "模拟数据设置";
        public const string MockDataSettingMockRulePage = "模拟数据设置_规则配置";
        public const string MockDataSettingMockFieldMappingRulePage = "模拟数据设置_规则映射";
        public const string MockDataSettingMockFieldClassifyPage = "模拟数据设置_类型划分";
        public const string MockDataGeneratePage = "模拟数据生成";
        public const string KnowledgeSettingPage = "知识点设置";
        public const string KnowledgeSettingLanguagePage = "知识点设置_语言设置";
        public const string KnowledgeSettingCustomParamPage = "知识点设置_自定义参数";
        public delegate void PageChangeHandler(Page page);
        public event PageChangeHandler? OnPageChange;
        public void Init(Frame frame)
        {
            PageFrame = frame;
            Pages = new Dictionary<string, Page>
            {
                //一级页面
                { FieldMappingPage, new Page(FieldMappingPage, "MainView.FieldMappingPage", FieldMappingPage) },
                { DependencyPage, new Page(DependencyPage, "MainView.DependencyPage", DependencyPage) },
                { GroupingSetsPage, new Page(GroupingSetsPage,"MainView.GroupingSetsPage", GroupingSetsPage) },
                { MockDataPage, new Page(MockDataPage, "MainView.MockDataPage",MockDataPage) },
                { KnowledgePage, new Page(KnowledgePage, "MainView.KnowledgePage",KnowledgePage) },
                { SettingPage, new Page(KnowledgePage,"MainView.SettingPage", SettingPage) },

                //二级页面
                { FieldMappingSettingPage, new Page(FieldMappingSettingPage, "FieldMapping.FieldMappingSettingPage", FieldMappingPage, "设置") },
                { MockDataSettingPage, new Page(MockDataSettingPage, "MockData.MockDataSettingPage", MockDataPage, "设置") },
                { MockDataGeneratePage, new Page(MockDataGeneratePage, "MockData.GeneratePage", MockDataPage, "生成数据") },
                { KnowledgeSettingPage, new Page(KnowledgeSettingPage, "Knowledge.KnowledgeSettingPage", KnowledgePage, "设置") },

                //三级页面
                { FieldMappingSettingDataSourcePage, new Page(FieldMappingSettingDataSourcePage, "FieldMapping.DataSourceSettingPage", FieldMappingPage, "设置", "数据源") },
                { FieldMappingSettingMappingRulePage, new Page(FieldMappingSettingMappingRulePage, "FieldMapping.MappingRuleSettingPage", FieldMappingPage, "设置", "映射规则") },
                { FieldMappingSettingModelPage, new Page(FieldMappingSettingModelPage, "FieldMapping.TemplateSettingPage", FieldMappingPage, "设置", "模板") },
                { MockDataSettingMockRulePage, new Page(MockDataSettingMockRulePage, "MockData.MockRuleSettingPage", MockDataPage, "设置", "规则配置") },
                { MockDataSettingMockFieldMappingRulePage, new Page(MockDataSettingMockFieldMappingRulePage, "MockData.MockFieldMappingRuleSettingPage", MockDataPage,  "设置", "规则映射") },
                { MockDataSettingMockFieldClassifyPage, new Page(MockDataSettingMockFieldClassifyPage, "MockData.MockFieldClassifySettingPage", MockDataPage,  "设置", "类型划分") },
                { KnowledgeSettingLanguagePage, new Page(KnowledgeSettingLanguagePage, "Knowledge.LanguageSettingPage", KnowledgePage,  "设置", "语言设置") },
                { KnowledgeSettingCustomParamPage, new Page(KnowledgeSettingCustomParamPage, "Knowledge.CustomParamSettingPage", KnowledgePage,  "设置", "自定义参数") },
            };
        }

        public void Navigate(string pageName)
        {
            Navigate(PageFrame, pageName);
        }

        public void Navigate(Frame? Frame, string pageName)
        {
            if (Frame == null) return;
            Pages.TryGetValue(pageName, out Page? page);
            if (page == null) return;

            Type? pageType = Type.GetType("FinalPointSet.View.Page." + page.FileName);
            if (pageType == null) return;
            System.Windows.Controls.Page? controlPage = Get(pageType);

            if (controlPage != null)
            {
                Frame.Navigate(controlPage);
                OnPageChange?.Invoke(page);
            }
        }

        /// <summary>
        /// 获取页面
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        public static System.Windows.Controls.Page? Get(Type type)
        {
            bufferPages.TryGetValue(type, out System.Windows.Controls.Page? controlPage);
            if (controlPage == null)
            {
                try
                {
                    controlPage = Activator.CreateInstance(type) as System.Windows.Controls.Page;
                }
                catch (Exception ex)
                {
                    Debug.Print(ex.Message);
                }
                if (controlPage == null) return null;

                Debug.Print($"PageUtil.Get(), 实例化新的Page={type.FullName}");
                bufferPages.Add(type, controlPage);
            }
            return controlPage;
        }

        public static bool Remove(Type type)
        {
            Debug.Print($"PageUtil.Remove(), 移除Page={type.FullName}");
            return bufferPages.Remove(type);
        }

        public static void RemoveAll()
        {
            Debug.Print($"PageUtil.RemoveAll(), 移除所有共[{bufferPages.Count}]个Page");
            bufferPages.Clear();
        }
    }

    public class Page
    {
        /// <summary>
        /// 页面名称
        /// </summary>
        public string Name { get; set; } = string.Empty;
        /// <summary>
        /// 文件名称
        /// </summary>
        public string FileName { get; set; } = string.Empty;
        /// <summary>
        /// 一级路径
        /// </summary>
        public string FirstPath { get; set; } = string.Empty;
        /// <summary>
        /// 二级路径
        /// </summary>
        public string SecondPath { get; set; } = string.Empty;
        /// <summary>
        /// 三级路径
        /// </summary>
        public string ThirdPath { get; set; } = string.Empty;
        public Page(string name, string fileName, string firstPah, string secondPath, string thirdPath)
        {
            Name = name;
            FileName = fileName;
            FirstPath = firstPah;
            SecondPath = secondPath;
            ThirdPath = thirdPath;
        }
        public Page(string name, string fileName, string firstPah, string secondPath)
        {
            Name = name;
            FileName = fileName;
            FirstPath = firstPah;
            SecondPath = secondPath;
        }
        public Page(string name, string fileName, string firstPah)
        {
            Name = name;
            FileName = fileName;
            FirstPath = firstPah;
        }
        public override string? ToString()
        {
            return $"Name={Name}, FileName={FileName}, FirstPath={FirstPath}, SecondPath={SecondPath}";
        }
    }
}
